package com.lesso.zgs.sunflower.domain.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.zgs.sunflower.domain.repository.EmployeeAttendanceRuleRepository;
import com.lesso.zgs.sunflower.model.EmployeeAttendanceRule;
import com.lesso.zgs.sunflower.pojo.dto.EmployeeAttendanceRuleDTO;
import com.lesso.zgs.sunflower.pojo.dto.EmployeeAttendanceRuleQueryDTO;
import com.lesso.zgs.sunflower.pojo.vo.EmployeeAttendanceRuleVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * EmployeeAttendanceRuleDomainService
 *
 * @author zgs
 * @since 2025-07-03
 */
@Service
public class EmployeeAttendanceRuleDomainService {
    private final EmployeeAttendanceRuleRepository employeeAttendanceRuleRepository;

    public EmployeeAttendanceRuleDomainService(EmployeeAttendanceRuleRepository employeeAttendanceRuleRepository) {
        this.employeeAttendanceRuleRepository = employeeAttendanceRuleRepository;
    }

    public Integer assignRule(EmployeeAttendanceRuleDTO dto) {
        EmployeeAttendanceRule employeeAttendanceRule = new EmployeeAttendanceRule();
        employeeAttendanceRule.setEmployeeId(dto.getEmployeeId());
        employeeAttendanceRule.setRuleCode(dto.getRuleCode());
        employeeAttendanceRule.setEffectiveDate(dto.getEffectiveDate() != null ? dto.getEffectiveDate() : LocalDate.now());
        employeeAttendanceRule.setExpiryDate(dto.getExpiryDate());
        return employeeAttendanceRuleRepository.save(employeeAttendanceRule);
    }

    public Integer cancelRule(Long id) {
        return employeeAttendanceRuleRepository.delete(id);
    }

    public Page<EmployeeAttendanceRuleVO> page(EmployeeAttendanceRuleQueryDTO dto) {
        return employeeAttendanceRuleRepository.getPageList(dto);
    }

    public List<EmployeeAttendanceRuleVO> getByEmployeeId(Long employeeId) {
        return employeeAttendanceRuleRepository.getByEmployeeId(employeeId);
    }
} 